package com.xjx.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xjx.dao.OrderSettingDao;
import com.xjx.pojo.OrderSetting;
import com.xjx.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Version v1.0
 * @ClassName:
 * @Description:
 * @Author:xjx
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置数据
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate > 0) {
                    //如果已经进行了预约设置，则进行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //没有进行预约设置，进行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    //根据月份查询对应的预约设置数据
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList();
        for (OrderSetting orderSetting : list) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("date", orderSetting.getOrderDate().getDate());//可以拿到当前时几号
            map1.put("number", orderSetting.getNumber());
            map1.put("reservations", orderSetting.getReservations());
            result.add(map1);
        }
        return result;
    }

    //根据日期修改预约设置数据
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行预约设置
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
        if (countByOrderDate>0){
            //当前日期已经进行预约设置
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }
        else
        {
            //当前数据没有进行预约设置，就插入
            orderSettingDao.add(orderSetting);
        }
    }
}
